package com.neusoft.neuiotms.dcvm.repository.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcvm.model.Config;
import com.neusoft.neuiotms.dcvm.model.QConfig;
import com.neusoft.neuiotms.dcvm.repository.CustomizedConfigRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedConfigRepositoryImpl extends AbstractRepository implements CustomizedConfigRepository {

    @Override
    public Page<Config> search(Pageable pageable, Searchable searchable) {
        final QConfig qConfig = QConfig.config;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Config> query = this.query().selectFrom(qConfig);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(Searchable searchable) {
        final QConfig qConfig = QConfig.config;
        final BooleanBuilder where = new BooleanBuilder();
        where.or(super.containsStr(qConfig.name, searchable, "kw"));
        where.or(super.containsStr(qConfig.code, searchable, "kw"));
        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return Config.class;
    }

}
